package pid

import (
	"fmt"
	"time"
)

// 动态需量控制算法参数
type DemandControl struct {
	BasePower       float64
	MaxPower        float64
	MinPower        float64
	PowerAdjustment float64
}

// 动态需量控制方法
func (dc *DemandControl) AdjustPower(currentPower float64, demand float64) float64 {
	adjustedPower := currentPower + dc.PowerAdjustment*(demand-currentPower)
	if adjustedPower > dc.MaxPower {
		adjustedPower = dc.MaxPower
	} else if adjustedPower < dc.MinPower {
		adjustedPower = dc.MinPower
	}
	return adjustedPower
}

func Demand() {
	battery := Battery{
		SOC:         99.95, // 初始电池SOC
		ChargePower: 0,     // 初始充电功率
		ChargeRate:  100,   // 充电速率
		PID: PID{
			Kp: 0.8,
			Ki: 0.4,
			Kd: 0.1,
		},
		DemandControl: DemandControl{
			BasePower:       50,
			MaxPower:        200,
			MinPower:        10,
			PowerAdjustment: 0.5,
		},
	}

	fmt.Printf("Initial SOC: %.2f%%\n", battery.SOC)

	for i := 0; i < 130; i++ {
		time.Sleep(1 * time.Second)      // 模拟时间流逝
		battery.UpdateChargePower(80, 1) // 目标 SOC 80%
		fmt.Printf("Updated SOC: %.2f%% ChargePower:%f\n", battery.SOC, battery.ChargePower)
	}
}
